package org.Panel;

import org.JsonMsg.Post;
import org.Tools.OnLineOP;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Date;

public class PostOP extends FaPanel{

    //标题
    JTextArea titleArea = new JTextArea();
    JScrollPane scrollTitle = new JScrollPane(titleArea);

    //内容
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);

    //修改按钮
    JButton updateButton = new JButton("修改");

    //发布按钮
    JButton publishButton = new JButton("发布");

    //删除按钮
    JButton DropButton = new JButton("删除");

    //返回按钮
    JButton reButton = new JButton("返回");

    //帖子
    Post post;
    //判断用户创建贴子标识
    int isCreat = 0;

    //提示文本
    JLabel Tip = new JLabel("");

    //置顶按钮
//    JButton TopButton = new JButton("置顶");

    //母界面接口
    PanelSize panelSize;
    PanelSkip panelSkip;

    public PostOP(PanelSize panelSize, PanelSkip panelSkip){

        isCreat = 1;
        init(panelSize, panelSkip);


    }

    public PostOP(PanelSize panelSize, PanelSkip panelSkip, Post post){

        isCreat = 0;
        this.post = post;
        init(panelSize, panelSkip);

    }

    private void init(PanelSize panelSize, PanelSkip panelSkip) {
        this.panelSize = panelSize;
        this.panelSkip = panelSkip;

        //属性设置
        PanelIndex = 6;
        setLayout(null);
        setBounds(0,0, panelSize.getWidth(), panelSize.getHeight());

        //标题
        scrollTitle.setBounds(10,10,getWidth()-20,50);
        Font font1 = new Font(null,0,30);
        titleArea.setFont(font1);
        if(isCreat==0) titleArea.setText(post.getTitle());

        //内容
        scrollPane.setBounds(10,100,getWidth()-100,500);
        //文本区,将文本区改为分行
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        Font font2 = new Font(null,0,25);
        textArea.setFont(font2);
        if(isCreat==0) textArea.setText(post.getContent());

        //返回按钮
        reButton.setBounds(10,650,80,50);
        reButton.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;
            @Override
            public void mouseClicked(MouseEvent e) {}

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1){
                    panelSkip.skip(3);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //更新按钮
        updateButton.setBounds(550,650,80,50);
        if(isCreat==1) updateButton.setSize(0,0);
        else updateButton.setSize(80,50);
        updateButton.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1){
                    String title = titleArea.getText();
                    String content = textArea.getText();
                    Date now = new Date();
                    String msg = "updatePost-{\"postId\":"+post.getPostId()+",\"title\":\""+title+"\",\"content\":\""+content+"\"}-"+MainPanel.userName;
                    System.out.println(msg);
                    String jsonmsg;
                    OnLineOP onLineOP = new OnLineOP();
                    try {
                        jsonmsg = onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    System.out.println(jsonmsg);

                    //操作完后跳转至个人界面
                    panelSkip.skip(3);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //删除按钮
        DropButton.setBounds(750,650,80,50);
        if(isCreat==1) DropButton.setSize(0,0);
        else DropButton.setSize(80,50);
        DropButton.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;
            @Override
            public void mouseClicked(MouseEvent e) {}

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1){
                    String title = titleArea.getText();
                    String content = textArea.getText();
                    String msg = "deletePostSt-"+post.getPostId()+"-jtd";
                    System.out.println(msg);
                    String jsonmsg;
                    OnLineOP onLineOP = new OnLineOP();
                    try {
                        jsonmsg = onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    System.out.println(jsonmsg);

                    //操作完后跳转至个人界面
                    panelSkip.skip(3);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //发布按钮
        publishButton.setBounds(950,650,80,50);
        if(isCreat==0) publishButton.setSize(0,0);
        else publishButton.setSize(80,50);
        publishButton.addMouseListener(new MouseAdapter() {
            //用于判定鼠标是否在按钮内
            int f=1;
            @Override
            public void mouseClicked(MouseEvent e) {}

            @Override
            public void mousePressed(MouseEvent e) {}

            @Override
            public void mouseReleased(MouseEvent e) {
                if(f==1){
                    String title = titleArea.getText();
                    String content = textArea.getText();
                    Date now = new Date();
                    String msg = "post-{\"title\":\""+title+"\",\"content\":\""+content+"\"}-"+MainPanel.userName;
                    System.out.println(msg);
                    String jsonmsg;
                    OnLineOP onLineOP = new OnLineOP();
                    try {
                        jsonmsg = onLineOP.sendone(msg);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    System.out.println(jsonmsg);

                    //操作完后跳转至个人界面
                    panelSkip.skip(3);

                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                f=1;
            }

            @Override
            public void mouseExited(MouseEvent e) {
                f=0;
            }
        });

        //如果是新建帖子则将清空输入框
        if(isCreat==1){
            titleArea.setText("");
            textArea.setText("");
        }


        //提示文本
        Tip.setBounds(600,610,100,30);
        Tip.setForeground(Color.RED);

        //添加
        add(reButton);
        add(updateButton);
        add(DropButton);
        add(publishButton);
        add(Tip);
        add(scrollTitle);
        add(scrollPane);
    }

    public void Run() {

//        System.out.println("PostOP");

    }

    public void updatePanel() {
        if(MainPanel.post==null) isCreat = 1;
        else {
            this.post = MainPanel.post;
            isCreat = 0;
        }
        //更新操作界面
        if(isCreat==1) {
            updateButton.setSize(0,0);
            DropButton.setSize(0,0);
            publishButton.setSize(80,50);
            //如果是新建帖子则将清空输入框
            titleArea.setText("");
            textArea.setText("");
        }else {
            titleArea.setText(post.getTitle());
            textArea.setText(post.getContent());
            updateButton.setSize(80,50);
            DropButton.setSize(80,50);
            publishButton.setSize(0,0);
        }

    }

}
